package com.t282.crm.service.impl;

import com.alibaba.excel.EasyExcel;
import com.t282.crm.entity.CstCustomer;
import com.t282.crm.mapper.CstCustomerMapper;
import com.t282.crm.service.CstCustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mr.Chen
 * @since 2021-05-19
 */
@Service
public class CstCustomerServiceImpl extends ServiceImpl<CstCustomerMapper, CstCustomer> implements CstCustomerService {

    @Override
    public void exportData(HttpServletResponse response) {
        //设置下载信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = "customer";
        //Content-disposition以下载方式打开
        response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");

        //查询要导出的数据
        List<CstCustomer> cstCustomers = baseMapper.selectList(null);
        //调用方法进行导出
        try {
            EasyExcel.write(response.getOutputStream(),CstCustomer.class).sheet("customer").doWrite(cstCustomers);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
